package _tema3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Ventana extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final int PORT = 7900;
	final String ADDRESS = "127.0.0.1";
	JComboBox combo;
	JList display;
	JButton login, logout, exit;
	DefaultListModel model;
	Cliente cliente;
	
	private Controlador controlador;
    
    public Ventana(Cliente c) {
    	super("Tema3");
 
		this.cliente = c;
		this.controlador = new Controlador(this, this.cliente);
		
    	setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		configurar();
		eventos();
    }
    
	
    private void eventos() {
		this.login.addActionListener(new ConectarEvent(this, this.controlador));
		this.logout.addActionListener(new DesconectarEvent(this, this.controlador));
		this.exit.addActionListener(new FinalizarEvent(this.controlador));
		this.combo.addActionListener(new EnviarEvent(this, this.controlador));
	}

	private void configurar() {
    	Container c = getContentPane();
    	
    	JPanel pnlNor = new JPanel();
    	pnlNor.setLayout(new GridLayout(1,2,5,5));
    	pnlNor.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
    	c.add(pnlNor, BorderLayout.NORTH); 
    	
    	JLabel lblJugador = new JLabel("Cargos o Funciones:");
    	pnlNor.add(lblJugador);
    	
    	combo = new JComboBox();
    	combo.setPreferredSize(new Dimension(120,18));
     	pnlNor.add(combo);    	
    	
    	model = new DefaultListModel();
      	display = new JList(model);
     	JScrollPane jsp = new JScrollPane(display);
    	c.add(jsp, BorderLayout.CENTER);
    	
    	JPanel pnlSur = new JPanel();
    	pnlSur.setLayout(new GridLayout(1,3,4,2));
    	pnlSur.setBorder(BorderFactory.createEtchedBorder());
    	c.add(pnlSur, BorderLayout.SOUTH);  
    	
    	login = new JButton("Conectar");
    	logout = new JButton("Desconectar");
    	logout.setSelected(true);
    	exit = new JButton("Finalizar");
    	exit.setSelected(true);
    	
    	logout.setEnabled(false);
  	    	
        pnlSur.add(login);
        pnlSur.add(logout);
        pnlSur.add(exit);
        
        setLocation(420,100);
        pack();
    }
	
	public void cargarCombo(ArrayList<String> cargos) {
		this.combo.removeAllItems();
		for (int i = 0; i < cargos.size(); i++)
			this.combo.addItem(cargos.get(i));
	}
	
	public void cargarLista(ArrayList<String> personas) {
		this.model.clear();
		for (int i = 0; i < personas.size(); i++)
			this.model.addElement(personas.get(i));
	}
	
	class ConectarEvent implements ActionListener {
		Ventana ventana;
		Controlador controlador;
		public ConectarEvent(Ventana ventana, Controlador controlador) {
			this.ventana = ventana;
			this.controlador = controlador;
		}
		public void actionPerformed(ActionEvent arg0) {
			this.ventana.login.setEnabled(false);
			this.ventana.logout.setEnabled(true);
			this.controlador.conectar();
		}
	}
	
	class DesconectarEvent implements ActionListener {
		Ventana ventana;
		Controlador controlador;
		public DesconectarEvent(Ventana ventana, Controlador controlador) {
			this.ventana = ventana;
			this.controlador = controlador;
		}
		public void actionPerformed(ActionEvent arg0) {
			this.ventana.login.setEnabled(true);
			this.ventana.logout.setEnabled(false);
			this.ventana.combo.removeAllItems();
			this.ventana.model.removeAllElements();
			this.controlador.desconectar();
		}
	}
	
	class FinalizarEvent implements ActionListener {
		Controlador controlador;
		public FinalizarEvent(Controlador controlador) {
			this.controlador = controlador;
		}
		public void actionPerformed(ActionEvent arg0) {
			this.controlador.desconectar();
			System.exit(0);
		}
	}
	
	class EnviarEvent implements ActionListener {
		Ventana ventana;
		Controlador controlador;
		public EnviarEvent(Ventana ventana, Controlador controlador) {
			this.ventana = ventana;
			this.controlador = controlador;
		}
		public void actionPerformed(ActionEvent arg0) {
			if (this.ventana.combo.getSelectedItem() != null)
				this.controlador.enviar((String)this.ventana.combo.getSelectedItem());
		}
	}
}
